import {jsonp} from "@/common/js/jsonp";
import {options} from "@/config/api-config/config";
import Singer from "@/common/js/singer";

export async function getSinger() {
    const url = 'https://c.y.qq.com/v8/fcg-bin/v8.fcg'

    const data = {
        channel: 'singer',
        page: 'list',
        key: 'all_all_all',
        pagesize: 100,
        pagenum: 1,
        hostUin: 0,
        needNewCode: 0,
        platform: 'yqq',
        g_tk: 1928093487,
        inCharset: 'utf-8',
        outCharset: 'utf-8',
        notice: 0,
        format: 'jsonp'
    }

    let result = await jsonp(url,data,options)

    return format_singer(result.data.list)
}

function format_singer(list) {
    let map = {
        hot: {
            title: '热门',
            items: []
        }
    }
    for (let i = 0; i < list.length; i++) {
        let item = list[i]
        const key = item.Findex
        if (i < 10)
            map.hot.items.push(new Singer({
                id: item.Fsinger_mid,
                name: item.Fsinger_name
            }))
        if (!map[key])
            map[key] = {
                title: key,
                items: []
            }
        map[key].items.push(new Singer({
            id: item.Fsinger_mid,
            name: item.Fsinger_name
        }))
    }
    let hot = []
    let ret = []
    hot.push(map.hot)
    for (let k in map){
        let val = map[k]
        if(val.title.match(/[a-zA-Z]/))
            ret.push(val)
    }
    ret.sort((a, b) => {
        return a.title.charCodeAt(0) - b.title.charCodeAt(0)
    })
    return hot.concat(ret)
}

